package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper mapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Role getOne(Long id) {
        Role role = mapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public PageInfo<Role> getPageInfo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> data = mapper.getData(qo);
        return new PageInfo<>(data);
    }

    @Override
    public void deleteRoleByOne(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteRoleAndPermission(Long id) {
        mapper.deleteRoleAndPermission(id);
    }

    @Override
    public void insert(Role role) {
        mapper.insert(role);
    }

    @Override
    public void update(Role role) {
        mapper.updateByPrimaryKey(role);
    }

    @Override
    public void insertPermission(Long roleId, Long permissionId) {
        mapper.insertRoleAndPermission(roleId,permissionId);
    }

    @Override
    public List<Permission> selectPermission(Long id) {
        List<Permission> list = permissionMapper.selectPermission(id);

        return list;
    }

    @Override
    public List<Role> getAll() {

        return mapper.selectAll();
    }
}
